package com.fc.mall.service.impl;/**
 * Created by Administrator on 2019/9/20.
 */

import com.fc.mall.dto.UmsMemberAccountParam;
import com.fc.mall.mapper.UmsMemberAccountChangeHistoryMapper;
import com.fc.mall.mapper.UmsMemberAccountMapper;
import com.fc.mall.model.UmsMemberAccount;
import com.fc.mall.model.UmsMemberAccountExample;
import com.fc.mall.model.UmsMemberAccountChangeHistory;
import com.fc.mall.service.UmsMemberAccountService;
import com.fc.mall.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 会员账户
 * @Description TODO
 * @Author Climb
 * @Date 2019/9/20
 **/
@Service
public class UmsMemberAccountServiceImpl implements UmsMemberAccountService{
    @Autowired
    private UmsMemberAccountMapper umsMemberAccountMapper;

    @Autowired
    private UmsMemberAccountChangeHistoryMapper accountChangeHistoryMapper;

    /**
     * 会员余额调整
     * @param param
     * @return
     */
    @Override
    public void updataAccount(UmsMemberAccountParam param) {
        param.setAmount(param.getAmount()*100);
        //找出该用户
        UmsMemberAccountExample example = new UmsMemberAccountExample();
        example.createCriteria().andMemberIdEqualTo(param.getMemberId());
        List<UmsMemberAccount>  accountList= umsMemberAccountMapper.selectByExample(example);
        if (!accountList.isEmpty()) {
            UmsMemberAccount account = accountList.get(0);
            //增加账户余额时
            if (param.getChangeType()){
                account.setAbleAmount(account.getAbleAmount()+param.getAmount());
                account.setTotalAmount(account.getTotalAmount()+param.getAmount());

                //插入历史记录
                insertAccountChangeHistory(param.getMemberId(),
                        Constants.AccountChangeType.ManualIncrease.getValue(),
                        param.getAmount().longValue(),
                        param.getRemark());

                //减少时账户余额大于等于所减数额
            }else if (!param.getChangeType() && param.getAmount().intValue()<=account.getAbleAmount().intValue()){
                account.setAbleAmount(account.getAbleAmount()-param.getAmount());
                account.setTotalAmount(account.getTotalAmount()-param.getAmount());

                //插入历史记录
                insertAccountChangeHistory(param.getMemberId(),
                        Constants.AccountChangeType.ManualReduce.getValue(),
                        param.getAmount().longValue(),
                        param.getRemark());
            }
            umsMemberAccountMapper.updateByPrimaryKey(account);
        }
    }

    /**
     * 插入一条account_change_history数据
     * @param memberId
     * @param type 会员账户类型
     * @param amount 变更金额（单位：分）
     * @param remark 备注
     */
    private void insertAccountChangeHistory(Long memberId, Integer type, Long amount, String remark){
        UmsMemberAccountChangeHistory umsMemberAccountChangeHistory =new UmsMemberAccountChangeHistory();
        umsMemberAccountChangeHistory.setMemberId(memberId);
        umsMemberAccountChangeHistory.setAccountType(Constants.AccountType.MemberAccount.getValue());
        umsMemberAccountChangeHistory.setType(type);
        umsMemberAccountChangeHistory.setAmount(amount);
        umsMemberAccountChangeHistory.setRemark(remark);
        umsMemberAccountChangeHistory.setCreateTime(new Date());
        accountChangeHistoryMapper.insert(umsMemberAccountChangeHistory);
    }
}
